package jhm;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleFinishedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import jhm.test.RobotTestBed;

/**
 * This to make sure the robot goes to the correct side of the map initially.
 * 
 * @author Jarret
 * 
 */
public class TestToSide extends RobotTestBed {
  // set to true if robot goes to right wall
  boolean rightWall = false;
  // set to true if robot goes to left wall
  boolean leftWall = false;
  // gets the battle field width
  int battleFieldWidth = battleFieldSpec.getWidth();

  /**
   * Specifies that SittingDuck and CounterBot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,jhm.CounterBot";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 20;
  }

  /**
   * Tests if robot is moving parallel to the right or left wall every round.
   * 
   * @param event The TurnEndedEvent
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // gets a snapshot of both CounterBot each turn
    IRobotSnapshot counterBot = event.getTurnSnapshot().getRobots()[1];
    // robot's x coordinate
    double xPos = counterBot.getX();

    // for both if statements, the robot's heading is checked since if the
    // robot is on the right wall, the robot will face up
    // if the robot is on the left wall, robot will face down
    // check if robot is on and parallel to right wall
    if (battleFieldWidth - 30 < xPos && Math.floor(counterBot.getBodyHeading()) <= 4) {
      rightWall = true;
    }

    // check if robot is on and parallel to left wall
    if (xPos < 30 && 177 <= Math.floor(counterBot.getBodyHeading())
        && 183 >= Math.floor(counterBot.getBodyHeading())) {
      leftWall = true;
    }

  }

  /**
   * Tests if the robot has gone to either side of the map every round.
   * 
   * @param event The RoundEndedEvent
   * 
   */
  @Override
  public void onBattleFinished(BattleFinishedEvent event) {
    assertTrue("Test Wall", rightWall || leftWall);
  }

  /**
   * Overrode this function to allow for robots to start in different initial positions every round.
   * 
   * @return True if the battle will be deterministic.
   */
  @Override
  public boolean isDeterministic() {
    return false;
  }

}
